package me.zhengjie.business.service.impl;

import lombok.extern.slf4j.Slf4j;
import me.zhengjie.business.domain.Jzdj;
import me.zhengjie.business.service.ZhsqZyService;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.StoredProcedureQuery;

/**
 * @description 转院
 * @author xiahongbo
 * @date 2025-03-11
 */
@Slf4j
@Service
public class ZhsqZyServiceImpl implements ZhsqZyService {

    @PersistenceContext
    private EntityManager em;

    /**
     * 机构转机构
     * @param sfz 身份证
     * @param newJgbm 新机构编码
     * @return
     */
    @Override
    @Transactional
    public void exeOtO(String sfz, String newJgbm) {
        log.info("[ZhsqZyServiceImpl - 机构转机构] - 开始");
        log.info("[ZhsqZyServiceImpl - 机构转机构] - sfz:{}", sfz);
        log.info("[ZhsqZyServiceImpl - 机构转机构] - newJgbm:{}", newJgbm);
        log.info("[ZhsqZyServiceImpl - 机构转机构] - 执行 PRC_MAKE_TRSF");

        StoredProcedureQuery storedProcedureQuery = this.em.createNamedStoredProcedureQuery("makeOtO");

        storedProcedureQuery.setParameter("IR_SFZ", sfz);
        storedProcedureQuery.setParameter("IR_NEW_JGBM", newJgbm);
        storedProcedureQuery.setParameter("IR_DYKSRQ", "");

        storedProcedureQuery.execute();

        int code = (int) storedProcedureQuery.getOutputParameterValue("OR_APPCODE");
        String msg = (String) storedProcedureQuery.getOutputParameterValue("OR_MESSAGE");

        log.info("[ZhsqZyServiceImpl - 机构转机构] - 结束 :code:{}，msg:{}", code, msg);
        if (code == -1) {
            throw new BadRequestException(msg);
        }
    }
}
